#pragma once

#include <fcntl.h>
#include <iostream>
#include <signal.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fstream>
#include <mutex>
#include <map>
#include <string>
#include <cstring>
#include "zlib.h"
//#include "capilot_module_interface.pb.h"
#include "interface/capilot_module_interface.pb.h"
using namespace std;

const int KPageSize = 4096;
// const int KRawDataBufferMaxSize = KPageSize * 1e4; // 40M

// const int GZBufferMaxSize = 1024 * 1024; // 1M


#pragma pack(1)
struct KDatHead
{
    float version;
    size_t protoContentSize;
    uint16_t commentLength;
    char reserve[200] = {0};
};
#pragma pack()

class CRecord
{
public:
    CRecord(const std::string &file_name, bool compress = false);
     // 拷贝构造函数
    CRecord(const CRecord& other);

    // 赋值运算符
    CRecord& operator=(const CRecord& other);

    // 禁止移动构造和赋值（如果资源不可转移）
    CRecord(CRecord&& other) = delete;
    CRecord& operator=(CRecord&& other) = delete;
    
    ~CRecord();

    void Init();

    void Record(const std::string &data);

    void Close();

    void SetProtoContent(const std::string &proto_content)
    {
        proto_content_ = proto_content;
    }

    std::string getFileName()
    {
        return file_name_;
    }

    static void SetVersion(capilot::interface::InterfaceReply reply);

    static void WriteSftVersion();
    static void WriteDiruanVersion();   
    static void WriteVinVersion();

    static string GetC2Version();
    static void SetC2Version();
    static bool getC3BVersion();
    static void AddDiruan(string &diruan);


private:
    void MoveToRawdataBufAndWriteWhenBufFull(const std::string &rdata);

private:
    int fd_ = -1;
    bool is_close_ = false;
    bool write_eror = false;
    std::string file_name_;
    // char *data_write_buf_ = NULL;
    std::unique_ptr<char[]> data_write_buf_;
    int data_buf_cur_size_{0};
    std::string proto_content_;

    gzFile os{nullptr};
    bool m_needCompress = false; //是否需要压缩

    int KRawDataBufferMaxSize; //不压缩缓存40M，压缩缓存1M

    static string SftVersion;
    static string DiruanVersion;
    static string VinVersion;

    static string C2Version;

    static string Version_total;
    static string Version_total_2;    
    
    static string ProtoInfo;
};